/**
 * Created by mac on 16/5/5.
 */
var net = require('net');

var chatServer = net.createServer();

function sendMsg(client,str) {
    client.write(str);
}

chatServer.on('connection', function(client) {
    var active = true
	console.log('connection')
    client.write('Hi!'); // 服务端向客户端输出信息，使用 write() 方法
    client.write("Hello 1");
	setTimeout(function (){
        if (active){
            client.write("Hello 2")
        }
    },2000)
    setTimeout(function (){
        if (active) {
            client.write("Hello 3")
        }
    },4000)
    setTimeout(function (){
        if (active) {
            client.end()
        }
    },6000)

    client.on("close",function ( had_error ) {
        console.log('客户端close', had_error );
    })
    client.on("end",function ( ) {
        active = false
        console.log('客户端end');
    })

    // 接受客户端的消息
    client.on("data",function (data) {
        console.log("客户端 发来的:",data.toString());
    })
});


chatServer.listen(9000, function() { 
  console.log('server is listening');
});